package com.syher.swagger.core.config;

import com.google.common.base.Predicates;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by shenyuhang on 2018/4/10.
 */
@Configuration
@EnableSwagger2
public class SwaggerConfigure {
    private static final Pattern SWAGGER_UI_BASE_PACKAGE_PATTERN = Pattern.compile("com.syher.*.controller");
    @Bean
    public Docket swaggerUI() {
        return new Docket(DocumentationType.SWAGGER_2)
                .useDefaultResponseMessages(false)
                .apiInfo(swaggerUIApiInfo())
                .select()
                .apis(input -> {
                            String name = input.declaringClass().getPackage().getName();
                            Matcher matcher = SWAGGER_UI_BASE_PACKAGE_PATTERN.matcher(name);
                            return matcher.matches();
                        }
                )
                .paths(Predicates.and(PathSelectors.regex("/.*"), Predicates.not(PathSelectors.regex("/error"))))
                .build();
    }

    private ApiInfo swaggerUIApiInfo() {
        return new ApiInfoBuilder()
                .title("Spring-Boot-Swagger")
                .description("Spring-Boot-Swagger App.")
                .termsOfServiceUrl("")
                .version("1.0")
                .build();
    }
}
